//
//  ReceivedCommentsViewController.m
//  Final
//
//  Created by student14 on 2021/12/18.
//  Copyright © 2021 Lenrony. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "../ModelController.h"
#import "ReceivedCommentsViewController.h"
#import "DetailViewController.h"
#import "./Views/MyCommentView.h"

@interface ReceivedCommentsViewController()

@property (strong, nonatomic) UIAlertController *alert;
@property (strong, nonatomic) NSMutableArray *rawUsers;
@property (strong ,nonatomic) NSMutableArray *receivedComments;
@property (strong, nonatomic) MyCommentView *commentView;

@property (strong, nonatomic) ModelController *mc;
@property float screenWidth;
@property float screenHeight;

@end

@implementation ReceivedCommentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _mc = [ModelController getInstance];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"gray.png"]];
    
    _screenWidth = self.view.frame.size.width;
    _screenHeight = self.view.frame.size.height;
    
    [self setupTitleView];
    [self setupCollectionView];
}

- (void)setupTitleView {
    UIView *tv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _screenWidth, 75)];
    [tv setBackgroundColor:[UIColor whiteColor]];
    tv.layer.cornerRadius = 1;
    [self.view addSubview:tv];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(_screenWidth/2-50, 44, 100, 20)];
    titleLabel.text = @"收到评论";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleLabel setTextColor:[UIColor colorWithRed:50/255.0 green:153/255.0 blue:50/255.0 alpha:1.0]];
    [titleLabel setFont:[UIFont systemFontOfSize:20]];
    [tv addSubview:titleLabel];
}

- (void)setupCollectionView {
    self.receivedComments = [[ModelController getInstance] getMyComment];
    if (self.receivedComments == nil) {
        [self showAlertWithTitle:@"联网失败" andText:@"请确认网络连接正常并刷新！"];
        self.receivedComments = [[NSMutableArray alloc] init];
    }
    if (self.receivedComments.count == 0) {
        [self showAlertWithTitle:@"当前没有评论" andText:@"没有任何收到评论..."];
        self.receivedComments = [[NSMutableArray alloc] init];
    }
    
    self.rawUsers = [[NSMutableArray alloc] init];
    for (Comment *comment in self.receivedComments) {
        [self.rawUsers addObject:[[ModelController getInstance] getRawUser:comment.u_id]];
    }
    
    self.commentView = [[MyCommentView alloc] initWithViewController: self
                                                         andRawUsers: self.rawUsers
                                                         andComments: self.receivedComments];
    [self.commentView setFrame:CGRectMake(0, 75, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height-75-[[UIApplication sharedApplication] statusBarFrame].size.height)];
    [self.commentView.myCommentsView setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height-75-[[UIApplication sharedApplication] statusBarFrame].size.height)];
    [self.view addSubview:self.commentView];
}

- (void)updateCollectionView {
    self.receivedComments = [[ModelController getInstance] getMyComment];
    if (self.receivedComments == nil) {
        [self showAlertWithTitle:@"联网失败" andText:@"请确认网络连接正常并刷新！"];
        self.receivedComments = [[NSMutableArray alloc] init];
    }
    if (self.receivedComments.count == 0) {
        [self showAlertWithTitle:@"当前没有评论" andText:@"没有任何收到评论..."];
        self.receivedComments = [[NSMutableArray alloc] init];
    }
    
    self.rawUsers = [[NSMutableArray alloc] init];
    for (Comment *comment in self.receivedComments) {
        [self.rawUsers addObject:[[ModelController getInstance] getRawUser:comment.u_id]];
    }
    
    [self.commentView updateCollectionViewWithComments:self.receivedComments
                                           andRawUsers:self.rawUsers];
}

- (void)showAlertWithTitle: (NSString*)title
                   andText: (NSString*)text
{
    self.alert = [UIAlertController alertControllerWithTitle:title message:text preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:self.alert animated:YES completion:nil];
    [self performSelector:@selector(hideAlert) withObject:self.alert afterDelay:1.5];
}

- (void)hideAlert
{
    [self.alert dismissViewControllerAnimated:YES completion:^{
        if(self.mc.cur_user != nil){
            [self viewWillAppear:YES];
        }
    }];
}

@end
